package com.wondertek.poms.search.service.impl;

import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.dao.po.ContentFee;
import com.wondertek.poms.search.service.IContentFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author evan0213
 * @description
 */
@Service
public class ContentFeeServiceImpl implements IContentFeeService {
	@Autowired
	private EntityManager entityManager;

	@Override
	public List<ContentFee> queryContentFee(String infoId, String prdContId) {
		List<ContentFee> list = new ArrayList<ContentFee>();
		StringBuffer sql = new StringBuffer(" ");
		sql.append(" select fee.TYPE,fee.POMS_ID,fee.PRICE,fee.ORDER_FLAG,fee.DISCOUNT,fee.SALE_CATEGORY, fee.CHANNEL_PACKAGE_ID " +
				"from CONTENT_FEE  fee where fee.POMS_ID in (" + infoId + "," + prdContId + ") and fee.START_TIME <= :startTime  and :endTime <= fee.END_TIME ");
		Query nativeQuery = entityManager.createNativeQuery(sql.toString());
		nativeQuery.setParameter("startTime", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		nativeQuery.setParameter("endTime", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		List<Object[]> resultList = nativeQuery.getResultList();
		for (Object[] obj : resultList) {
			ContentFee fee = new ContentFee();
			fee.setType(StringUtil.nullToInteger(obj[0]));
			fee.setPomsId(StringUtil.nullToLong(obj[1]));
			fee.setPrice(StringUtil.null2Str(obj[2]));
			fee.setOrderFlag(StringUtil.null2Str(obj[3]));
			fee.setDiscount(StringUtil.null2Str(obj[4]));
			fee.setSaleCategory(StringUtil.nullToInteger(obj[5]));
			fee.setChannelPackageId(obj[6] == null ? null : StringUtil.nullToLong(obj[6]));
			list.add(fee);
		}
		return list;
	}
}
